package com.lg.thread;

public class Depot {

	
	  private volatile int capacity;    // 仓库的容量
	    private volatile int size;        // 仓库的实际数量
		public Depot(int capacity) {
			super();
			this.capacity = capacity;
			this.size = 0;
		}
	    
		public  void produce() {
			
			try {
				while(true) {
					if(size >=capacity) {
						synchronized ("tomcat") {
							"tomcat".wait();
						}
					}
					Thread.sleep(3000);
					size++;
					System.out.println(Thread.currentThread().getName()+"  produce"+"size"+size);
					if(size >0) {
					synchronized ("tomcat") {
						
						"tomcat".notifyAll();
					}
					}
					
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	    
		public  void consume() {
			try {
				while(true) {
					if(size <= 0) {
						synchronized ("tomcat") {
							"tomcat".wait();
						}
					}
					Thread.sleep(3000);
					size--;
					System.out.println(Thread.currentThread().getName()+"  consume"+ size);
					if(size <=0) {
						
						synchronized ("tomcat") {
							"tomcat".notifyAll();
						}
					}
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
